package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetMealMapper setMealMapper;
    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        return shoppingCartMapper.list(shoppingCart);
    }

    /**
     * 新增购物车
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //判断当前要加入购物车的数据是否已经存在
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if (list != null && list.size() > 0){
            //如果商品已经存在了只需要进行number+1(update操作)
            ShoppingCart shopping = list.get(0);
            shopping.setNumber(shopping.getNumber() + 1);
            //然后进行修改操作即可update
            shoppingCartMapper.update(shopping);
            return;
        }

        //如果商品不存在进行加入操作,判断时添加什么商品信息
        //补全购物车中公共数据
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCart.setNumber(1);//第一次添加购物车的商品数量设置为1
        //判断此次添加的时菜品还是套餐,并补充剩余数据
        Long dishId = shoppingCart.getDishId();
        if(dishId != null){
            //此次操作为添加菜品
            Dish dish = dishMapper.findById(dishId);
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
            shoppingCart.setAmount(dish.getPrice());
        }else{
            //否则就是添加的套餐
            Setmeal setmeal = setMealMapper.findById(shoppingCart.getSetmealId());
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setAmount(setmeal.getPrice());
        }

        shoppingCartMapper.insert(shoppingCart);
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        shoppingCartMapper.clean(BaseContext.getCurrentId());
    }

    /**
     * 减少购物车中的某一个菜品
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);//获取当前需要修改的菜品
        ShoppingCart shopping = list.get(0);
        //判断菜品数据是否大于1
        if(shopping.getNumber() > 1){
            //如果大于1件
            shopping.setNumber(shopping.getNumber() - 1);
            shoppingCartMapper.update(shopping);
        }else {
            //从购物车中删除当前数据即可
            shoppingCartMapper.deleteById(shopping.getId());
        }
    }
}
